#include "QQEntryView.h"

Q_TAKEOVER_NATIVEEVENT_CPP(QQEntryView, QQEntryView::mAppBar)
QQEntryView::QQEntryView(QWidget* parent)
	: QDialog(parent)
{
	this->setFixedSize(QSize(340, 450));

	mAppBar = new ElaAppBar(this);
	mAppBar->setIsFixedSize(true);
	mAppBar->setIsDefaultClosed(false);
	mAppBar->setWindowButtonFlags(ElaAppBarType::CloseButtonHint);


	QRegularExpressionValidator* accountValidator = new QRegularExpressionValidator(QRegularExpression("^[1-9][0-9]{4,10}$"), this);
	QRegularExpressionValidator* numberValidator = new QRegularExpressionValidator(QRegularExpression("^(?:(?:\\+|00)86)?1(?:(?:3[\\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\\d])|(?:9[1589]))\\d{8}$"), this);
	QRegularExpressionValidator* passwordValidator = new QRegularExpressionValidator(QRegularExpression("^\\S*(?=\\S{6,12})(?=\\S*\\d)(?=\\S*[A-Z])(?=\\S*[a-z])(?=\\S*[!@#$%^&*? ])\\S*$"), this);

	int inputHeight = 40;

	mAvatar = new ElaIconButton(QPixmap(":/QQClient/QQ.png"), this);
	mAvatar->setFixedSize(QSize(100, 100));
	mAvatar->setBorderRadius(50);
	QHBoxLayout* avatarLayout = new QHBoxLayout;
	avatarLayout->setContentsMargins(0, 0, 0, 0);
	avatarLayout->setSpacing(0);
	avatarLayout->addStretch();
	avatarLayout->addWidget(mAvatar);
	avatarLayout->addStretch();

	mLoginAccount = new ElaLineEdit(this);
	mLoginAccount->setPlaceholderText(tr("Please input account"));
	mLoginAccount->setValidator(accountValidator);
	mLoginAccount->setMinimumHeight(inputHeight);
	mLoginPassword = new ElaLineEdit(this);
	mLoginPassword->setPlaceholderText(tr("Please input password"));
	mLoginPassword->setValidator(passwordValidator);
	mLoginPassword->setEchoMode(QLineEdit::EchoMode::Password);
	mLoginPassword->setMinimumHeight(inputHeight);
	mLoginButton = new ElaPushButton(this);
	mLoginButton->setText(tr("login"));
	mLoginButton->setFlat(true);
	mLoginButton->setFocusPolicy(Qt::NoFocus);
	mLoginButton->setCursor(Qt::PointingHandCursor);
	mLoginButton->setMinimumHeight(inputHeight);
	mLoginOption = new QDialogButtonBox(this);
	mLoginOption->addButton(new ElaPushButton(tr("register"), this), QDialogButtonBox::ButtonRole::ActionRole);
	mLoginOption->addButton(new ElaPushButton(tr("findPassword"), this), QDialogButtonBox::ButtonRole::ActionRole);
	mLoginOption->addButton(new ElaPushButton(tr("rcode"), this), QDialogButtonBox::ButtonRole::ActionRole);
	mLoginOption->setCenterButtons(true);
	mLoginOption->setMinimumHeight(inputHeight);

	QWidget* login = new QWidget(this);
	QVBoxLayout* loginLayout = new QVBoxLayout(login);
	loginLayout->addLayout(avatarLayout);
	loginLayout->addWidget(mLoginAccount);
	loginLayout->addWidget(mLoginPassword);
	loginLayout->addWidget(mLoginButton);
	loginLayout->addWidget(mLoginOption);
	loginLayout->setContentsMargins(40, 0, 40, 0);
	loginLayout->setSpacing(0);

	mRegisterNickname = new ElaLineEdit(this);
	mRegisterNickname->setPlaceholderText(tr("Please input nickname"));
	mRegisterNickname->setMinimumHeight(inputHeight);
	mRegisterNumber = new ElaLineEdit(this);
	mRegisterNumber->setPlaceholderText(tr("Please input phone number"));
	mRegisterNumber->setValidator(numberValidator);
	mRegisterNumber->setMinimumHeight(inputHeight);
	mRegisterPassword = new ElaLineEdit(this);
	mRegisterPassword->setPlaceholderText(tr("Please input password"));
	mRegisterPassword->setValidator(passwordValidator);
	mRegisterPassword->setEchoMode(QLineEdit::EchoMode::Password);
	mRegisterPassword->setMinimumHeight(inputHeight);
	mRegisterButton = new ElaPushButton(this);
	mRegisterButton->setText(tr("register"));
	mRegisterButton->setFlat(true);
	mRegisterButton->setFocusPolicy(Qt::NoFocus);
	mRegisterButton->setCursor(Qt::PointingHandCursor);
	mRegisterButton->setMinimumHeight(inputHeight);
	mRegisterOption = new QDialogButtonBox(this);
	mRegisterOption->addButton(new ElaPushButton(tr("login"), this), QDialogButtonBox::ButtonRole::ActionRole);
	mRegisterOption->setCenterButtons(true);
	mRegisterOption->setMinimumHeight(inputHeight);

	QWidget* registe = new QWidget(this);
	QVBoxLayout* registerLayout = new QVBoxLayout(registe);
	registerLayout->addWidget(mRegisterNickname);
	registerLayout->addWidget(mRegisterNumber);
	registerLayout->addWidget(mRegisterPassword);
	registerLayout->addWidget(mRegisterButton);
	registerLayout->addWidget(mRegisterOption);
	registerLayout->setContentsMargins(40, 0, 40, 0);
	registerLayout->setSpacing(0);

	mFindPasswordAccount = new ElaLineEdit(this);
	mFindPasswordAccount->setPlaceholderText(tr("Please input account"));
	mFindPasswordAccount->setValidator(accountValidator);
	mFindPasswordAccount->setMinimumHeight(inputHeight);
	mFindPasswordNumber = new ElaLineEdit(this);
	mFindPasswordNumber->setPlaceholderText(tr("Please input phone number"));
	mFindPasswordNumber->setValidator(numberValidator);
	mFindPasswordNumber->setMinimumHeight(inputHeight);
	mFindPasswordButton = new ElaPushButton(this);
	mFindPasswordButton->setText(tr("findPassword"));
	mFindPasswordButton->setFlat(true);
	mFindPasswordButton->setFocusPolicy(Qt::NoFocus);
	mFindPasswordButton->setCursor(Qt::PointingHandCursor);
	mFindPasswordButton->setMinimumHeight(inputHeight);
	mFindPasswordOption = new QDialogButtonBox(this);
	mFindPasswordOption->addButton(new ElaPushButton(tr("login"), this), QDialogButtonBox::ButtonRole::ActionRole);
	mFindPasswordOption->setCenterButtons(true);
	mFindPasswordOption->setMinimumHeight(inputHeight);

	QWidget* findPassword = new QWidget(this);
	QVBoxLayout* findPasswordLayout = new QVBoxLayout(findPassword);
	findPasswordLayout->addWidget(mFindPasswordAccount);
	findPasswordLayout->addWidget(mFindPasswordNumber);
	findPasswordLayout->addWidget(mFindPasswordButton);
	findPasswordLayout->addWidget(mFindPasswordOption);
	findPasswordLayout->setContentsMargins(40, 0, 40, 0);
	findPasswordLayout->setSpacing(0);

	mRcodeImage = new ElaInteractiveCard(this);
	mRcodeImage->setMinimumSize(QSize(256, 256));
	mRcodeImage->setCardPixmapSize(256, 256);
	mRcodeImage->setCardPixmapBorderRadius(5);
	mRcodeImage->setCardPixMode(ElaCardPixType::PixMode::RoundedRect);
	QString text = qq_settings->getQRCodeEntryBaseUrl() + QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
	mRcodeImage->setCardPixmap(QPixmap::fromImage(QQSystemApi::generateQrCode(text)));
	mRcodeOption = new QDialogButtonBox(this);
	mRcodeOption->addButton(new ElaPushButton(tr("login"), this), QDialogButtonBox::ButtonRole::ActionRole);
	mRcodeOption->setCenterButtons(true);
	mRcodeOption->setMinimumHeight(inputHeight);

	QWidget* rcode = new QWidget(this);
	QVBoxLayout* rcodeLayout = new QVBoxLayout(rcode);
	rcodeLayout->addWidget(mRcodeImage);
	rcodeLayout->addWidget(mRcodeOption);
	rcodeLayout->setContentsMargins(40, 0, 40, 0);
	rcodeLayout->setSpacing(0);

	mLayout = new QStackedLayout(this);
	mLayout->addWidget(login);
	mLayout->addWidget(registe);
	mLayout->addWidget(findPassword);
	mLayout->addWidget(rcode);
	this->setLayout(mLayout);

	connect(mAppBar, &ElaAppBar::closeButtonClicked, mAppBar, &ElaAppBar::closeWindow);
	connect(mLoginButton, &ElaPushButton::clicked, this, &QQEntryView::doEntryInfoSubmit);
	connect(mRegisterButton, &ElaPushButton::clicked, this, &QQEntryView::doRegisterInfoSubmit);
	connect(mFindPasswordButton, &ElaPushButton::clicked, this, &QQEntryView::doFindPasswordInfoSubmit);
	connect(mRcodeImage, &QPushButton::clicked, this, &QQEntryView::doRefreshRcode);
	connect(mLoginOption, &QDialogButtonBox::clicked, this, &QQEntryView::doChangeView);
	connect(mRegisterOption, &QDialogButtonBox::clicked, this, &QQEntryView::doChangeView);
	connect(mFindPasswordOption, &QDialogButtonBox::clicked, this, &QQEntryView::doChangeView);
	connect(mRcodeOption, &QDialogButtonBox::clicked, this, &QQEntryView::doChangeView);
}

QQEntryView::~QQEntryView()
{

}

void QQEntryView::loadLoggedInfo(QPixmap pixmap, const QString& account, const QString& password)
{
	mAvatar->setPixmap(pixmap);
	mLoginAccount->setText(account);
	mLoginPassword->setText(password);
}

void QQEntryView::showMessageBar(const QString& message)
{
	ElaMessageBar::warning(ElaMessageBarType::PositionPolicy::Top, QString(""), message, 2000, this);
}

void QQEntryView::doEntryInfoSubmit()
{
	QString account = mLoginAccount->text();
	QString password = mLoginPassword->text();
	emit entryInfoSubmit(account, password);
}

void QQEntryView::doRegisterInfoSubmit()
{
	QString nickname = mRegisterNickname->text();
	QString phone = mRegisterNumber->text();
	QString password = mRegisterPassword->text();
	emit registerInfoSubmit(nickname, phone, password);
}

void QQEntryView::doFindPasswordInfoSubmit()
{
	QString account = mFindPasswordAccount->text();
	QString phone = mFindPasswordNumber->text();
	emit findPasswordInfoSubmit(account, phone);
}

void QQEntryView::doRefreshRcode()
{
	QString text = qq_settings->getQRCodeEntryBaseUrl() + QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
	mRcodeImage->setCardPixmap(QPixmap::fromImage(QQSystemApi::generateQrCode(text)));
}

void QQEntryView::doChangeView(QAbstractButton* button)
{
	int index = mLayout->currentIndex();
	QDialogButtonBox* option = qobject_cast<QDialogButtonBox*>(sender());
	if (option == mLoginOption)
	{
		mLoginPassword->clear();
		QList<QAbstractButton*> buttons = mLoginOption->buttons();
		mLayout->setCurrentIndex(buttons.indexOf(button) + 1);
	}
	else if (option == mRegisterOption)
	{
		mRegisterNickname->clear();
		mRegisterNumber->clear();
		mRegisterPassword->clear();
		mLayout->setCurrentIndex(0);
	}
	else if (option == mFindPasswordOption)
	{
		mFindPasswordAccount->clear();
		mFindPasswordNumber->clear();
		mLayout->setCurrentIndex(0);
	}
	else if (option == mRcodeOption)
	{
		doRefreshRcode();
		mLayout->setCurrentIndex(0);
	}
}
